package com.starinside.wp.utils;

import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.WebAppResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.beetl.ext.web.WebRenderExt;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URL;

@Configuration
public class BeetlConfigFactory implements WebRenderExt{

    @Value(value = "${spring.beetl.prefix}")
    private String prefix;

    @Value(value = "${spring.beetl.suffix}")
    private String suffix;

    @Value(value = "${spring.beetl.content.type}")
    private String contentType;

    @Value(value = "${spring.beetl.root}")
    private String root;

    @Value(value = "${spring.beetl.order}")
    private int order;

    @Value(value = "${system.context.path}")
    private String systemContextPath;


    @Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
        try {
            Resource resource = patternResolver.getResource(root);
            URL url = resource.getURL();
            String path = null;


            if (url.getProtocol().equals("jar")) {
                String pwd = System.getProperty("user.dir");
                String tmpDir = "templates";
                JarFileUtils.copyDirFromJarByNIO(url, tmpDir, pwd);
                path = new File(pwd, tmpDir).getAbsolutePath();
            } else {
                path = resource.getFile().getPath();
            }

            WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(path);
            beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader);
            ParamsLoadUtils.setSystemContextPath(systemContextPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return beetlGroupUtilConfiguration;
    }

    @Bean(name = "beetlViewResolver")
    public BeetlSpringViewResolver getBeetlSpringViewResolver(
            @Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
//    beetlSpringViewResolver.setPrefix(prefix);
        beetlSpringViewResolver.setSuffix(suffix);
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(order);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        return beetlSpringViewResolver;
    }

    @SuppressWarnings("unused")
    @Override
    public void modify(Template template, GroupTemplate gt, HttpServletRequest request,
        HttpServletResponse response) {
      template.binding("sysVersion","�׽�о1.0");
      template.binding("contextPath", "");
    }



}
